#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>

mqserver::BindingManagerPtr bmp;

class BindingTest : public testing::Environment 
{
public:
    virtual void SetUp() override { 
        bmp = std::make_shared<mqserver::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override {
        // bmp->clear();
        std::cout << "clear finish!\n";
    }
};


// TEST(binding_test, insert_test)
// {
//     bmp->bind("exch1", "mq1", "news.music.#", true);
//     bmp->bind("exch1", "mq2", "news.sport.#", true);
//     bmp->bind("exch1", "mq3", "news.gossip.#", true);
//     bmp->bind("exch2", "mq1", "news.music.pop", true);
//     bmp->bind("exch2", "mq2", "news.sport.football", true);
//     bmp->bind("exch2", "mq3", "news.gossip.hot", true);
//     ASSERT_EQ(bmp->size(), 6); 
// }

// TEST(binding_test, select_test)
// {
//     ASSERT_EQ(bmp->exists("exch1", "mq1"), true);
//     ASSERT_EQ(bmp->exists("exch1", "mq2"), true);
//     ASSERT_EQ(bmp->exists("exch1", "mq3"), true);
//     ASSERT_EQ(bmp->exists("exch2", "mq1"), true);
//     ASSERT_EQ(bmp->exists("exch2", "mq2"), true);
//     ASSERT_EQ(bmp->exists("exch2", "mq3"), true);

//     mqserver::BindingPtr bp = bmp->getBinding("exch1", "mq1");
//     ASSERT_NE(bp.get(), nullptr);
//     ASSERT_EQ(bp->exchange_name_, "exch1");
//     ASSERT_EQ(bp->msgqueue_name_, "mq1");
//     ASSERT_EQ(bp->binding_key_, "news.music.#");
//     ASSERT_EQ(bp->durable_, true);
// }

// TEST(binding_test, select_exchange_test)
// {
//     mqserver::MsgQueueBindingMap mqbm = bmp->getExchangeBindings("exch1");
//     ASSERT_EQ(mqbm.size(), 3);
//     ASSERT_NE(mqbm.find("mq1"), mqbm.end());
//     ASSERT_NE(mqbm.find("mq2"), mqbm.end());
//     ASSERT_NE(mqbm.find("mq3"), mqbm.end());
// }

// TEST(binding_test, remove_msgqueue_test)
// {
//     bmp->removeMsgQueueBindings("mq1");
//     ASSERT_EQ(bmp->exists("exch1", "mq1"), false);
//     ASSERT_EQ(bmp->exists("exch2", "mq1"), false);
// }


// TEST(binding_test, remove_exchange_test)
// {
//     bmp->removeExchangeBindings("exch1");
//     ASSERT_EQ(bmp->exists("exch1", "mq1"), false);
//     ASSERT_EQ(bmp->exists("exch1", "mq2"), false);
//     ASSERT_EQ(bmp->exists("exch1", "mq3"), false);
// }

/*
exch2, mq2
exch2, mq3

sqlite> select * from binding_table;
    exch2|mq2|news.sport.football|1
    exch2|mq3|news.gossip.hot|1
*/

TEST(binding_test, remove_exchange_test)
{
    bmp->removeExchangeBindings("exch1");
    ASSERT_EQ(bmp->exists("exch1", "mq1"), false);
    ASSERT_EQ(bmp->exists("exch1", "mq2"), false);
    ASSERT_EQ(bmp->exists("exch1", "mq3"), false);
}

TEST(binding_test, recovery_test)
{
    ASSERT_EQ(bmp->exists("exch1", "mq1"), false);
    ASSERT_EQ(bmp->exists("exch1", "mq2"), false);
    ASSERT_EQ(bmp->exists("exch1", "mq3"), false);
    ASSERT_EQ(bmp->exists("exch2", "mq1"), false);
    ASSERT_EQ(bmp->exists("exch2", "mq2"), true);
    ASSERT_EQ(bmp->exists("exch2", "mq3"), true);
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest());
    (void)RUN_ALL_TESTS();
    return 0;
}